package com.tianji.learning.controller;


import com.tianji.common.domain.dto.PageDTO;
import com.tianji.learning.domain.vo.LearningPlanPageVO;
import com.tianji.learning.domain.vo.LearningPlanVO;
import com.tianji.learning.domain.dto.LearningPlanDTO;
import com.tianji.common.domain.query.PageQuery;
import com.tianji.common.utils.UserContext;
import com.tianji.learning.domain.enums.LessonStatus;
import com.tianji.learning.domain.po.LearningLesson;
import com.tianji.learning.domain.vo.LearningLessonVO;
import com.tianji.learning.service.ILearningLessonService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import org.checkerframework.checker.units.qual.A;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * <p>
 * 学生课程表 前端控制器
 * </p>
 *
 * @author author
 * @since 2025-03-19
 */
@Api(tags = "我的课表相关接口")
@RestController
@RequestMapping("/lessons")
@RequiredArgsConstructor
public class LearningLessonController {

    private final ILearningLessonService lessonService;
    /**
     * 分页查询我的课表
     * @param pageQuery
     * @return
     */
    @GetMapping("/page")
    @ApiOperation("分页查询我的课表")
    public PageDTO<LearningLessonVO> pageDTO(PageQuery pageQuery) {
        return lessonService.pageQueryMyLessons(pageQuery);
    }

    /**
     * 删除课表中的某课程
     */
    @DeleteMapping("/{courseId}")
    @ApiOperation("删除课表中的某课程")
    public void PageDTO(@PathVariable Long courseId) {
        lessonService.deleteLesson(courseId);
    }

    /**
     * 查询我最近正在学习的课程
     */
    @GetMapping("/now")
    @ApiOperation("查询我最近正在学习的课程")
    public LearningLessonVO getNow() {
        return lessonService.queryNow();
    }

    /**
     * 统计课程学习人数
     * @param courseId 课程id
     * @return 学习人数
     */
    @GetMapping("/{courseId}/count")
    @ApiOperation("统计课程学习人数")
    public Integer countLearningLessonByCourse(@PathVariable("courseId") Long courseId) {
        return lessonService.lambdaQuery()
                .eq(LearningLesson::getCourseId, courseId)
                .in(LearningLesson::getStatus, List.of(0, 1, 2))
                .count();
    }

    /**
     * 校验接口
     * @param courseId
     * @return
     */
    @GetMapping("/{courseId}/valid")
    @ApiOperation("校验接口")
    public Long isLessonValid(@PathVariable("courseId") Long courseId) {
        return lessonService.getIsLessonValid(courseId);
    }

    /**
     * 根据id查询指定课程的学习状态
     */
    @GetMapping("/{courseId}")
    @ApiOperation("根据id查询指定课程的学习状态")
    public LessonStatus getLearnStatus(@PathVariable("courseId") Long courseId) {
        return lessonService.lambdaQuery()
                .eq(LearningLesson::getCourseId, courseId)
                .eq(LearningLesson::getUserId, UserContext.getUser())
                .one().getStatus();
    }

    /**
     * 查询我的学习计划
     */
    @ApiOperation("查询我的学习计划")
    @GetMapping("/plans")
    public LearningPlanPageVO queryMyPlans(PageQuery pageQuery) {
        return lessonService.queryMyPlans(pageQuery);
    }

    /**
     * 创建我的学习计划
     */
    @ApiOperation("创建我的学习计划")
    @PostMapping("/plans")
    public void createPlans(@RequestBody LearningPlanDTO planDTO)  {
        lessonService.createPlans(planDTO);
    }

}
